package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;
    // 分页查询
    @Override
    public PageResult pageQuery(EmpQueryParam param) {
        //1.构建分页对象
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());

        //2.调用service层方法分页查询
        //2.1 动态的构建多条件
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(param.getName() != null && !param.getName().isEmpty(),Emp::getName, param.getName())
                .eq(param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());

        //2.2 分页查询
        page = page(page,wrapper);

        //3.查询部门数据
        List<Dept> depts = deptService.list();
        List<Emp> emps = page.getRecords();
        for (Emp emp : emps) {
            depts.forEach(dept -> {
                if (dept.getId() == emp.getDeptId()) {
                    emp.setDeptName(dept.getName());
                }
            });
        }
        return new PageResult(page.getTotal(), page.getRecords());
    }

    // 新增员工
    @Override
    public void saveEmpandEmpexpr(Emp emp) {
        //1.保存员工信息
        save(emp);
        // 2.保存员工工作经历信息
        List<EmpExpr> exprLists = emp.getExprList();
        exprLists.forEach(expr -> {
            expr.setEmpId(emp.getId());
            empExprService.save(expr);
        });

    }
    //  批量删除员工
    @Override
    public void deleteEmpAndEmpexpr(List<Integer> ids) {
        //1.批量删除员工信息
        removeBatchByIds(ids);
        // 2.批量删除员工工作经历信息
        List<Emp> empList = listByIds(ids);
        for (Emp emp : empList) {
            LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(EmpExpr::getEmpId,emp.getId());
            empExprService.remove(wrapper);
        }
    }
    // 根据id查询员工信息和工作经历信息
    @Override
    public Emp getEmpAndEmpExprById(Integer id) {
        //1.查询员工信息
        Emp emp = getById(id);
        // 2.查询员工工作经历信息
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,id);
        List<EmpExpr> empExprs = empExprService.list(wrapper);
        emp.setExprList(empExprs);
        return emp;
    }
    //  更新员工信息
    @Override
    public void updateEmpAndEmpexpr(Emp emp) {
        //1.更新员工信息
        updateById(emp);
        // 2.删除员工工作经历信息
        emp.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,emp.getId());
        empExprService.remove(wrapper);
        //3.添加员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        for (EmpExpr empExpr : exprList) {
            empExpr.setEmpId(emp.getId());
            empExprService.save(empExpr);
        }
    }
}
